<template>
  <a-modal
    title="服务授权"
    width="900px"
    centered
    :visible="selectServeVisible"
    :maskClosable="false"
    @ok="submit()"
    @cancel="close"
  >
  <div class="serve-content">
    <productList :selectProduct="selectProduct" 
    :options="options" :isSelectTime="true" 
    @selectCheckedArr = "selectCheckedArr"
    v-if="show"></productList>
  </div>
  </a-modal>
</template>

<script>
import productList from "components/base/productList";
import { user,unitOperation } from "@/api/user";
export default {
  components: {
    productList
  },
  props: {
    selectServeVisible: {
      type: Boolean,
      default: false
    },
    //关闭弹框
    closeFunc: {
      type: Function,
      default: () => {}
    },
    id: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      options: [
       
      ],
      show:false,
      //选中的服务
      selectProduct:[],
      product:[]
    };
  },
  watch: {
    selectServeVisible() {
      this.getServiceList()
    }
  },
  methods: {
    //获取所有服务
    getServiceList(){
      this.show = false
      this.selectProduct=[]
      user
        .getServiceList(this.id)
        .then(res => {
        this.options = res.data.data
        this.options.forEach((item)=>{
          if(item.isActive === true){
            this.selectProduct.push(item.id)
          }
        })
        this.show = true
        })
        .catch(err => {
          this.$message.destroy();
          this.$message.error(err);
        })
        .finally(() => {});
    },
    //更新产品日期时间
    updateProductDate(val){
      var param = {
          expire:val.expire,
          mainId:this.id,
          systemId:val.id
        }
        unitOperation
        .updateProductDate(param)
        .then(res => {
          this.$message.destroy();
          this.$message.success(res.data.msg);
        })
        .catch(err => {
          this.$message.destroy();
          this.$message.error(err);
        });
    },
    //获取选中的产品id
    selectCheckedArr(val){
      this.product = val
    },
    //关闭弹框
    close() {
      this.$emit("closeFunc");
    },
    submit() {
      this.product.forEach((item)=>{
        item.systemId = item.id
      })
      var param = {
        mainId:this.id,
        list:this.product
      }
      user
        .openServe(param)
        .then(res => {
          this.$message.destroy();
          this.$message.success(res.data.msg);
          this.$emit("closeFunc");
        })
        .catch(err => {
          this.$message.destroy();
          this.$message.error(err);
        });
    }
  }
};
</script>

<style lang="stylus" scoped>
.serve-content {
  max-height: 450px;
  overflow: hidden;
  overflow-y: auto;
}
</style>